//
//  MyClass.m
//  SlideImageGameTutorial
//
//  Created by xing lion on 11-8-31.
//  Copyright 2011年 IPhoneGameTutorials. All rights reserved.
//

#import "CompleteLayer.h"
#import "PlayLayer.h"
#import "SceneManager.h"
#import "FileManager.h"
#import "leveLayer.h"
#import "levelayerManager.h"

@implementation CompleteLayer

@synthesize picNum = _picNum;

-(void) saveDone
{

    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setMessage:@"MM Save Success!"];
    [dialog addButtonWithTitle:@"OK"];
    [dialog show];   
    [dialog release];
}

-(void)savetoAlbum
{
    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString* name = [NSString stringWithFormat:@"%d.jpg",_picNum];
    
    UIImage* img = [UIImage imageNamed:name];
    UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
    [[FileManager sharedInstance] saveDictionaryTofile];

    [self performSelectorOnMainThread:@selector(saveDone) withObject:nil waitUntilDone:YES];
    [pool release];
}


-(void) saveMMtoPhoen:(id)sender
{

  //  CCMenu* menu = (CCMenu*)[self getChildByTag:3];
    
    CCMenuItemFont* item = (CCMenuItemFont*)sender;
    
    [item setIsEnabled:NO];
    NSMutableDictionary* dictionary =  [[FileManager sharedInstance] getMMPicStatus];
    
    NSString* filename = [NSString stringWithFormat:@"%d.jpg",_picNum];
    [dictionary setValue:@"YES" forKey:filename];
    [self performSelectorInBackground:@selector(savetoAlbum) withObject:[NSNumber numberWithInt:1]];

}


-(void)gotoMMlistView:(id)sender
{
	CCLayer *layer = [levelayerManager node];
    //[(PlayLayer*)layer gotonextMM :1];
	[SceneManager go: layer];

}

-(void) retrymm:(id)sender

{
    CCLayer *la = [PlayLayer node];
    PlayLayer* tmp = (PlayLayer*)la;
    
    [tmp gotonextMM:_picNum];
	[SceneManager go: la];

}

-(void) nextMM:(id)sender
{
    if (_picNum >= 88) {
        return;
    }
    _picNum++;
    CCLayer *la = [PlayLayer node];
    PlayLayer* tmp = (PlayLayer*)la;
    
    [tmp gotonextMM:_picNum];
	[SceneManager go: la];
    
}

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

-(void) setPicNumtolayer:(int)picNums Step:(int)step
{

    _picNum = picNums;
    NSString *mmfilename = [NSString stringWithFormat:@"%d.jpg",picNums];
    CCSprite* sp = [CCSprite spriteWithFile:mmfilename];
    
    sp.position = ccp(160, 240);
    [self addChild:sp z:1 tag:1];
   // CGSize s = [[CCDirector sharedDirector] winSize];
    CCLayerColor* ly = [CCLayerColor layerWithColor:ccc4(0xeb, 0xeb, 0xeb, 0x80) width:300 height:180 ];
    
    ly.position = ccp(10, 130);
    
    if (step <= 35) {
        
        
        CCSprite * spxx = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx.position = ccp(115, 163);
        
        CCSprite * spxx2 = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx2.position = ccp(145, 163);
        
        CCSprite * spxx3 = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx3.position = ccp(175, 163);
        
        [ly addChild:spxx];
        [ly addChild:spxx2];
        [ly addChild:spxx3];
        
    }
    else if(step >=36 && step < 45 )
    {
        CCSprite * spxx = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx.position = ccp(115, 163);
        
        CCSprite * spxx2 = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx2.position = ccp(145, 163);
        
        
        [ly addChild:spxx];
        [ly addChild:spxx2];

    }
    
    else 
    {
    
        
        CCSprite * spxx = [CCSprite spriteWithFile:@"sc_star.png"];
        
        spxx.position = ccp(115, 163);
        
        
        
        [ly addChild:spxx];

    }
    
    
    NSString *score = [NSString stringWithFormat:@"Your Score:"];
    
	CCLabelTTF *solvedLabel = [CCLabelTTF labelWithString:score fontName:@"Marker Felt" fontSize:20];
    
    solvedLabel.anchorPoint = ccp(0,0);
	solvedLabel.position = ccp(10,150 );
    [solvedLabel setColor:ccc3(0x0, 0x0, 0x0)];
    [ly addChild:solvedLabel];
    
    
    NSString *youStep = [NSString stringWithFormat:@"Your Use Step:%d",step];
    
	CCLabelTTF *solvedLabel2 = [CCLabelTTF labelWithString:youStep fontName:@"Marker Felt" fontSize:20];
    
    solvedLabel2.anchorPoint = ccp(0,0);
	solvedLabel2.position = ccp(10,80 );
    [solvedLabel2 setColor:ccc3(0x0, 0x0, 0x0)];
    [ly addChild:solvedLabel2];
    
    
 //   CCSprite *returnmmlist = [CCSprite spriteWithFile:@"menu.png"];
    
//    CCMenuItemSprite *itemmm = [CCMenuItemSprite itemFromNormalSprite:returnmmlist selectedSprite:nil disabledSprite:nil target:self selector:@selector(gotoMMlistView:)];
    
    CCMenuItemImage* itemmm = [CCMenuItemImage itemFromNormalImage:@"menu2.png" selectedImage:@"menu2.png" target:self selector:@selector(gotoMMlistView:)]; 
    
    
     CCMenu *menulist = [CCMenu menuWithItems: itemmm,nil];
    [menulist setPosition:ccp(220, 100)];
    [ly addChild:menulist z:4 tag:4];
    
    CCMenuItemFont *itemsave = [CCMenuItemFont itemFromString: @"Save MM" target: self selector:@selector(saveMMtoPhoen:)];
    
    [itemsave setFontSize:20];
    [itemsave setFontName:@"Marker Felt"];
    [itemsave setColor:ccc3(0x0, 0x0, 0x0)];
    [itemsave setTag:1];
    
    CCMenuItemFont *itemretry = [CCMenuItemFont itemFromString: @"Retry" target: self selector:@selector(retrymm:)];
    
    [itemretry setFontSize:20];
    [itemretry setFontName:@"Marker Felt"];
    [itemretry setColor:ccc3(0x0, 0x0, 0x0)];
    [itemretry setTag:2];
    
    
    CCMenuItemFont *itmenext = [CCMenuItemFont itemFromString: @"Next MM" target: self selector:@selector(nextMM:)];
    
    [itmenext setFontSize:20];
    [itmenext setFontName:@"Marker Felt"];
    [itmenext setColor:ccc3(0x0, 0x0, 0x0)];
    [itmenext setTag:3];
    
    
    CCMenu *menu = [CCMenu menuWithItems: itemsave, itemretry, itmenext,nil];
    
    [menu alignItemsHorizontallyWithPadding:30];
    [menu setPosition:ccp(150, 20)];
    [ly addChild:menu z:3 tag:3];
    [self addChild:ly z:2 tag:2];
    
    
    NSMutableDictionary* dictionary =  [[FileManager sharedInstance] getMMPicStatus];
    
    NSString* status =  (NSString*)[dictionary objectForKey:mmfilename];
    
    if ([status isEqualToString:@"YES"]) {
        
        [itemsave setIsEnabled:NO];
    }
    
    [dictionary setValue:[NSNumber numberWithInt:step ] forKey:[mmfilename stringByAppendingFormat:@"step"]];
    
    [[FileManager sharedInstance] saveDictionaryTofile];
    
}

@end
